#include "../include/ui_core/signals.h"
#include <QPainter>
#include <qdebug.h>
#include <QColor>

Signals::Signals()

{

}

QPixmap Signals::GetPixmap(const int ping)
{
    return GetSignalPixmap(GetColor(ping),GetLineNum(ping));
}

QColor Signals::GetColor(const int ping )//获取颜色
{
    if(ping<=10)
        return QColor(0x6a,0x6a,0xff);
    else if(ping<=20)
        return QColor(0x28,0x94,0xff);
    else if(ping<=30)
           return QColor(0x00,0xff,0xff);
    else if(ping<=40)
        return QColor(0x9a,0xff,0x02);
    else if(ping<=50)
        return QColor(0xe1,0xe1,0x00);
    else if(ping<=60)
        return QColor(0xea,0xc1,0x00);
    else if(ping<=70)
        return QColor(0xe8,0x00,0xe8);
    else if(ping<=80)
        return QColor(0xff,0x00,0x80);
    else
        return QColor(0xea,0x00,0x00);
}

int Signals::GetLineNum(const int ping)//获取线条数量
{
    if(ping<=25)
        return 4;      //4格信号
    else if(ping<=50)
        return 3;
    else if(ping<=75)
        return 2;
    else if(ping<=100)
        return 1;
    else
        return 0;    //没有信号
}

QPixmap Signals::GetSignalPixmap(const QColor &color,const int linenum)//获取信号位图
{
    qDebug()<<linenum;

    int recWidth = 40;       //绘制区域的宽度和高度
    int recHeight = 30;
    int signalWidth = recWidth/4;
    int signalHeight = recHeight/4;
    QPixmap pixmap(recWidth,recHeight+2);
    QBrush brush(color);
    pixmap.fill(QColor(0xd9,0xd9,0xd9,0));
    QPainter painter(&pixmap);
    painter.setPen(QColor(0xd9,0xd9,0xd9));

    painter.drawRect(0,            recHeight-signalHeight*1, signalWidth-3,signalHeight*1);
    painter.drawRect(signalWidth*1,recHeight-signalHeight*2, signalWidth-3,signalHeight*2);
    painter.drawRect(signalWidth*2,recHeight-signalHeight*3, signalWidth-3,signalHeight*3);
    painter.drawRect(signalWidth*3,recHeight-signalHeight*4, signalWidth-3,signalHeight*4);

    switch(linenum)
    {
        case 4:
            painter.setBrush(brush);
            painter.drawRect(0,            recHeight-signalHeight*1, signalWidth-3,signalHeight*1);
            painter.drawRect(signalWidth*1,recHeight-signalHeight*2, signalWidth-3,signalHeight*2);
            painter.drawRect(signalWidth*2,recHeight-signalHeight*3, signalWidth-3,signalHeight*3);
            painter.drawRect(signalWidth*3,recHeight-signalHeight*4, signalWidth-3,signalHeight*4);
        break;
        case 3:
            painter.setBrush(brush);
            painter.drawRect(0,            recHeight-signalHeight*1, signalWidth-3,signalHeight*1);
            painter.drawRect(signalWidth*1,recHeight-signalHeight*2, signalWidth-3,signalHeight*2);
            painter.drawRect(signalWidth*2,recHeight-signalHeight*3, signalWidth-3,signalHeight*3);
        break;
        case 2:
            painter.setBrush(brush);
            painter.drawRect(0,            recHeight-signalHeight*1, signalWidth-3,signalHeight*1);
            painter.drawRect(signalWidth*1,recHeight-signalHeight*2, signalWidth-3,signalHeight*2);
        break;
        case 1:
            painter.setBrush(brush);
            painter.drawRect(0,            recHeight-signalHeight*1, signalWidth-3,signalHeight*1);
        break;
    }
    return pixmap;
}
